package com.mall.common.service.impl;

import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mall.common.bean.ArticleCategoryBean;
import com.mall.common.bean.Order;
import com.mall.common.bean.SearchFilter;
import com.mall.common.dao.ArticleCategoryDao;
import com.mall.common.domain.content.ArticleCategoryDomain;
import com.mall.common.hibernate.Finder;
import com.mall.common.service.ArticleCategoryService;

/**
 * Service实现类 - 文章类别
 */
@Service
@Transactional(readOnly=true)
public class ArticleCategoryServiceImpl extends BaseServiceImpl<ArticleCategoryDomain, Long> implements ArticleCategoryService {

	
	@Resource
	public void setBaseDao(ArticleCategoryDao article_categoryDao) {
		super.setBaseDao(article_categoryDao);
	}

	@Override
	@Transactional(readOnly=true)
	public List<ArticleCategoryDomain> getTopList() {
		List<SearchFilter> filters = new LinkedList<SearchFilter>();
		filters.add(SearchFilter.isNull("parent"));
		return this.findList(null, null, filters, null);
	}
	
	@Override
	@Transactional(readOnly=true)
	@Cacheable(value="articleCategory", key="'articleCategory'+#parentCode+','+#count")
	public List<ArticleCategoryBean> getArticleCategory(String parentCode, Integer count) {
		Finder finder = Finder.create("select new com.mall.common.bean.ArticleCategoryBean(bean1) from ArticleCategoryDomain bean join bean.children bean1 where 1=1");
		if(StringUtils.isNotBlank(parentCode)){
			finder.append(" and bean.code =:parentCode");
			finder.setParam("parentCode", parentCode);
		}
		if(count != null){
			finder.setMaxResults(count);
		}
		finder.setOrder(Order.asc("bean1.priority"));
		return finder.findHQLList();
	}

	@Override
	@CacheEvict(value="articleCategory",allEntries=true)
	@Transactional(propagation=Propagation.REQUIRED)
	public void persist(ArticleCategoryDomain entity) {
		super.persist(entity);
	}

	@Override
	@CacheEvict(value={"articleCategory","article"},allEntries=true)
	@Transactional(propagation=Propagation.REQUIRED)
	public void remove(Long[] ids) {
		super.remove(ids);
	}

	@Override
	@CacheEvict(value="articleCategory",allEntries=true)
	@Transactional(propagation=Propagation.REQUIRED)
	public ArticleCategoryDomain update(ArticleCategoryDomain entity) {
		return super.update(entity);
	}
	
}